package cn.wolfcode.trip.app.filter;

import cn.wolfcode.trip.app.util.UploadUtil;
import org.apache.commons.io.FileUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

public class ImageFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 类型转换,用于获取url
        HttpServletRequest req = (HttpServletRequest) request;
        // 获取url
        String url = req.getRequestURI();
        // 获取模拟的图片服务器的路径
        File file = new File(UploadUtil.BASE_PATH, url);
        // 判断图片是否存在
        if (file.exists()) {
            byte[] bytes = FileUtils.readFileToByteArray(file);
            response.getOutputStream().write(bytes);
        } else {
            chain.doFilter(request,response);
        }

    }








    public void destroy() {

    }

    public void init(FilterConfig filterConfig) throws ServletException {

    }

}
